# Contributor: rubicon <rubicon@mailo.com>
# Maintainer: Celeste <cielesti@protonmail.com>
pkgname=ocaml-erm_xmpp
_pkgname=xmpp
pkgver=0_git20220404
_commit=e54d54e142ac9770c37e144693473692bf473530
pkgrel=2
pkgdesc="XMPP protocol implementation for OCaml"
url="https://github.com/hannesm/xmpp"
arch="all !riscv64 !loongarch64" # ocaml
license="BSD-3-Clause"
depends_dev="
	$pkgname=$pkgver-r$pkgrel
	ocaml-base64-dev
	ocaml-erm_xml-dev
	ocaml-mirage-crypto-dev
	"
makedepends="
	$depends_dev
	ocaml
	ocamlbuild
	ocaml-findlib
	ocaml-compiler-libs
	ocaml-camlp4-dev
	"
subpackages="$pkgname-dev"
source="$pkgname-$pkgver.tar.gz::https://github.com/hannesm/xmpp/archive/$_commit/xmpp-$_commit.tar.gz"
builddir="$srcdir/$_pkgname-$_commit"
_ocamldir=usr/lib/ocaml

# 32-bit archs
case "$CARCH" in
	arm*|x86) options="$options textrels" ;;
esac

build() {
	# Workaround ocamlbuild terminating with error code 10
	if [ "$CARCH" = ppc64le ]; then
		ulimit -Hs unlimited
		ulimit -Ss 65536
	fi

	ocaml setup.ml -configure --prefix /usr
	ocaml setup.ml -build
}

check() {
	ocaml setup.ml -test
}

package() {
	export OCAMLFIND_DESTDIR="$pkgdir/$_ocamldir"
	export OCAMLFIND_LDCONF=ignore
	mkdir -vp "$OCAMLFIND_DESTDIR"

	ocaml setup.ml -install
}

dev() {
	default_dev

	cd "$pkgdir"

	local path; find $_ocamldir \( \
			-name '*.cmt' -o \
			-name '*.cmti' -o \
			-name '*.ml' -o \
			-name '*.mli' \
		\) | while read -r path
	do
		amove "$path"
	done
}

prepare_py_provides() {
	local datadir="${subpkgdir:-$pkgdir}"
	local pkgbasedir=${pkgbasedir:-"$startdir/pkg"}
	local controldir="$pkgbasedir"/.control.${subpkgname:-$pkgname}

	options_has "!tracedeps" && return 0

	ocaml4-abuild-find provides \
		"$datadir"/usr/lib/ocaml \
		"$controldir" \
		"$pkgver-r$pkgrel"
}

scan_python3_dependency() {
	local controldir="$2" datadir="$3"

	ocaml4-abuild-find requires \
		"$datadir"/usr/lib/ocaml \
		"$controldir"
}

sha512sums="
84e5b165d0257cfbbc83e888adeca2233cca201f66b7ac4d661a1fee56290816d3c36f8793bfae8ca10e52acd76baa4b29ca45e29da2701976d0b676a600252a  ocaml-erm_xmpp-0_git20220404.tar.gz
"
